package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.DeviceCurrentComboPlanRoadDO;
import com.yfp.operation.domain.dto.DamageDTO;
import com.yfp.operation.mapper.DeviceCurrentComboPlanRoadMapper;
import com.yfp.operation.service.DeviceCurrentComboPlanRoadService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【device_current_combo_plan_road】的数据库操作Service实现
 * @createDate 2024-04-17 17:41:45
 */
@Service
public class DeviceCurrentComboPlanRoadServiceImpl extends ServiceImpl<DeviceCurrentComboPlanRoadMapper, DeviceCurrentComboPlanRoadDO> implements DeviceCurrentComboPlanRoadService {

    @Resource
    private DeviceCurrentComboPlanRoadMapper deviceCurrentComboPlanRoadMapper;

    /**
     * 根据设备id集合查询设备当前组合计划货道
     *
     * @param machineIds
     * @return
     */
    @Override
    public List<DeviceCurrentComboPlanRoadDO> getComboNumByMachineIds(List<String> machineIds) {
        if (CollUtil.isEmpty(machineIds)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<DeviceCurrentComboPlanRoadDO> query = Wrappers.lambdaQuery(DeviceCurrentComboPlanRoadDO.class);
        query.in(DeviceCurrentComboPlanRoadDO::getMachineId, machineIds);
        List<DeviceCurrentComboPlanRoadDO> list = this.list(query);
        return list;
    }

    /**
     * 根据设备集合统计设备的库存数量
     *
     * @param deviceCurrentComboPlanRoadDOS 设备集合
     * @return 每个设备的库存数量
     */
    @Override
    public Map<String, Integer> statisticalQuantity(List<DeviceCurrentComboPlanRoadDO> deviceCurrentComboPlanRoadDOS) {
        // 根据设备id分组，并统计每个设备库存数量
        return deviceCurrentComboPlanRoadDOS.stream()
                .collect(Collectors.groupingBy(DeviceCurrentComboPlanRoadDO::getMachineId))
                .entrySet().stream().collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> entry.getValue().stream()
                                .map(DeviceCurrentComboPlanRoadDO::getComboNum)
                                .mapToInt(Integer::parseInt)
                                .sum()
                ));
    }

    /**
     * 根据设备id查询设备当前组合计划货道
     *
     * @param machineId 设备id
     * @return 设备当前组合计划货道
     */
    @Override
    public List<DeviceCurrentComboPlanRoadDO> getAllByMachineId(String machineId) {
        LambdaQueryWrapper<DeviceCurrentComboPlanRoadDO> query = Wrappers.lambdaQuery(DeviceCurrentComboPlanRoadDO.class);
        query.eq(DeviceCurrentComboPlanRoadDO::getMachineId, machineId);
        List<DeviceCurrentComboPlanRoadDO> list = this.list(query);
        return list;
    }

    /**
     * 根据设备id删除设备当前组合计划货道
     *
     * @param damageId 设备id
     * @return 是否删除成功
     */
    @Transactional
    @Override
    public Boolean deleteById(List<String> damageId) {
        if (CollUtil.isEmpty(damageId)) {
            return Boolean.FALSE;
        }
        LambdaQueryWrapper<DeviceCurrentComboPlanRoadDO> query = Wrappers.lambdaQuery(DeviceCurrentComboPlanRoadDO.class);
        query.in(DeviceCurrentComboPlanRoadDO::getId, damageId);
        return this.remove(query);
    }

    /**
     * 报损处理
     *
     * @param damageDTOS 报损数据
     * @return 是否删除成功
     */
    @Transactional
    @Override
    public Boolean damageHandle(List<DamageDTO> damageDTOS) {
        List<String> damageId = damageDTOS.stream().map(damageDTO -> damageDTO.getId()).collect(Collectors.toList());
        return this.deleteById(damageId);
    }

    @Transactional
    @Override
    public void deleteByMachineId(String deviceId) {
        deviceCurrentComboPlanRoadMapper.deleteByMachineId(deviceId);
    }

    @Transactional
    @Override
    public void insert(DeviceCurrentComboPlanRoadDO item) {
        deviceCurrentComboPlanRoadMapper.insert(item);
    }

    /**
     * 获取设备货道库存
     * @param machineId 设备id
     * @return
     */
    @Override
    public List<DeviceCurrentComboPlanRoadDO> roadInfo(String machineId) {
        return deviceCurrentComboPlanRoadMapper.roadInfo(machineId);
    }

    @Transactional
    @Override
    public void updateRoad(DeviceCurrentComboPlanRoadDO dccpr) {
        deviceCurrentComboPlanRoadMapper.updateRoad(dccpr);
    }

    @Transactional
    @Override
    public void updateComboNum(DeviceCurrentComboPlanRoadDO aDo) {
        deviceCurrentComboPlanRoadMapper.updateComboNum(aDo);
    }

    @Transactional
    @Override
    public void batchUpdate(ArrayList<DeviceCurrentComboPlanRoadDO> dccprList) {
        deviceCurrentComboPlanRoadMapper.batchUpdate(dccprList);
    }

    @Override
    public void insertAll(List<DeviceCurrentComboPlanRoadDO> comboList) {
        deviceCurrentComboPlanRoadMapper.insertAll(comboList);
    }


}




